package com.pda.yuanjing.pages.inbilln.add;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.pda.yuanjing.R;
import com.pda.yuanjing.database.DataBaseControl;
import com.pda.yuanjing.database.InStore;
import com.pda.yuanjing.dialog.product.ProductDialog;
import com.public_module.basemvp.activity.BaseActivity_noMVP;
import com.public_module.tools.DateUtils;
import com.public_module.tools.SharedPreferencesManger;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BillAddActivity extends BaseActivity_noMVP {
    private String TAG = "BillAddActivity";

    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.tvbill)
    EditText tvbill;
    @BindView(R.id.product)
    TextView product;
    @BindView(R.id.edcode)
    EditText edcode;
    @BindView(R.id.logine)
    Button logine;
    @BindView(R.id.listview)
    SwipeMenuListView listview;

    ProductDialog productDialog;
    String pstr = "";
    int cnum = 0;
    ProductAdapter productAdapter = null;
    List<InStore> list = new ArrayList<>();

    protected void initdata() {
        tvTitle.setText("离线入库建单");
        tvbill.setText(DateUtils.stampToDateTime(new Date().getTime()));
        View headView = getLayoutInflater().inflate(R.layout.head_iscanf, null);
        listview.addHeaderView(headView, null, false);
        listview.setHeaderDividersEnabled(false);
        productAdapter = new ProductAdapter(this, list);
        listview.setAdapter(productAdapter);

        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
                deleteItem.setWidth(100);
                deleteItem.setTitle("删除");
                deleteItem.setTitleSize(16);
                deleteItem.setTitleColor(Color.WHITE);
                deleteItem.setBackground(R.color.red);
                menu.addMenuItem(deleteItem);
            }
        };
        listview.setMenuCreator(creator);
        listview.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        list.remove(position);
                        productAdapter = new ProductAdapter(BillAddActivity.this, list);
                        listview.setAdapter(productAdapter);
                        break;
                }
                return false;
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addinbill);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @OnClick({R.id.iv_title_left, R.id.logine, R.id.tv_right, R.id.product})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.logine:
                if (tvbill.getText().toString().length() == 0) {
                    showMsg("请填写单号");
                    return;
                }
                if (edcode.getText().toString().length() == 0) {
                    showMsg("请填写数量");
                    return;
                }
                if (Integer.parseInt(edcode.getText().toString()) <= 0) {
                    showMsg("应扫数必须大于0");
                    return;
                }
                if (pstr.length() == 0) {
                    showMsg("请填写产品");
                    return;
                }
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getProductID().equals(pstr)) {
                        showMsg("请填选择不同产品");
                        return;
                    }
                }
                InStore inStore = new InStore();
                inStore.setBillID(tvbill.getText().toString());
                inStore.setBillCode(tvbill.getText().toString());
                inStore.setCreateTime(DateUtils.stampToDate(new Date().getTime()));
                inStore.setDesStoreID(SharedPreferencesManger.getStoreID());
                inStore.setDesStoreName(SharedPreferencesManger.getStoreName());
                inStore.setProductID(pstr);
                inStore.setProductName(product.getText().toString());
                inStore.setCaseBoxNum(cnum);
                inStore.setYnum(Integer.parseInt(edcode.getText().toString()));
                inStore.setSnum(0);
                inStore.setBillListID(pstr + "-" + DateUtils.stampToDateTime(new Date().getTime()));
                list.add(inStore);
                productAdapter.notifyDataSetChanged();
                pstr = "";
                product.setText("");
                edcode.setText("");
                break;
            case R.id.tv_right:
                if (list.size() == 0) {
                    showMsg("当前订单无任何产品信息");
                    return;
                }
                for (int i = 0; i < list.size(); i++) {
                    DataBaseControl.addInBill(list.get(i));
                }
                showMsg("添加订单成功");
                finish();
                break;
            case R.id.product:
                productDialog = new ProductDialog(mContext, R.style.CommentDialog, new ProductDialog.PriorityListener() {
                    @Override
                    public void setActivityText(String str, String str1, int num) {
                        product.setText(str);
                        pstr = str1;
                        cnum = num;
                        productDialog.dismiss();
                    }
                });
                WindowManager.LayoutParams lp = productDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                productDialog.show();
                break;
        }
    }
}
